package com.atguigu.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.realtime.app.BaseApp;
import com.atguigu.realtime.common.ConstantTopic;
import com.atguigu.realtime.util.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.PatternTimeoutFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: DWMUserJumpDetailApp
 * @Description:
 * @Author: kele
 * @Date: 2021/4/20 13:52
 *
 * 计算跳出
 * 跳出就是用户成功访问了网站的入口页面(例如首页)后就退出，不再继续访问网站的其它页面。
 *
 * 思路：通过flink自带的CEP对流的数据进行匹配
 *      1、第一次进入，在page中last_page_id = null
 *      2、之后在一定时间内，没有访问
 **/
public class DWMUserJumpDetailApp extends BaseApp {

    public static void main(String[] args) {

        new DWMUserJumpDetailApp()
                .init(30001,
                        2,
                        "DWMUserJumpDetailApp",
                        ConstantTopic.DWD_PAGE,
                        "DWMUserJumpDetailApp");
    }


    @Override
    protected void run(StreamExecutionEnvironment env, DataStreamSource<String> dataStreamSource) {
        //测试数据
        /*dataStreamSource =
                env.fromElements(
                "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000} ",
                      "{\"common\":{\"mid\":\"101\"},\"page\":{\"page_id\":\"home\"},\"ts\":10001} ",
                      "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"home\"},\"ts\":10000}",
                      "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"home\"},\"ts\":18000}",
                      "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":" +
                                "\"home\"},\"ts\":48999} ",
                       "{\"common\":{\"mid\":\"102\"},\"page\":{\"page_id\":\"good_list\",\"last_page_id\":" +
                                "\"detail\"},\"ts\":50000} "
                );*/

        //通过cep匹配出跳出数据
        DataStream<String> jumpDataByCEP = getJumpDataByCEP(dataStreamSource);

        jumpDataByCEP.print();

        //将数据写入到kafka中
        send2kafka(jumpDataByCEP);

    }

    private void send2kafka(DataStream<String> jumpDataByCEP) {

        jumpDataByCEP.addSink(MyKafkaUtil.getKafkaSink(ConstantTopic.DWM_USER_JUMP_DETAIL));

    }

    /**
     * 1、kafka读取数据，将其中的string转jsonobject
     * 2、设置事件时间作为水印
     * 3、按照每个用户进行keyby
     * 4、数据进行cep，
     *******      --1、第一条数据的lastpage为空
     *******      --2、下一条数据的lastpage还是空        --则第一条数据就是跳出的   （ 匹配得到）
     *******      --2、假如一个用户点了一下，再也没有点，   --通过设置超时时间来判断跳出 （超时得到）
     * 5、将 匹配的数据 和 超时的数据 进行connect返回
     */
    private DataStream<String> getJumpDataByCEP(DataStreamSource<String> dataStreamSource) {


        KeyedStream<JSONObject, String> stream = dataStreamSource
                .map(JSON::parseObject)
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                                .withTimestampAssigner((element, ts) -> element.getLong("ts"))
                )
                .keyBy(obj -> obj.getJSONObject("common").getString("mid"));

        Pattern<JSONObject, JSONObject> pattern = Pattern
                //先是last_page == null
                .<JSONObject>begin("go_in")
                .where(new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject value) throws Exception {

                        String last_page = value.getJSONObject("page").getString("last_page_id");
                        return last_page == null || last_page.isEmpty();
                    }
                })
                .next("next")
                .where(new SimpleCondition<JSONObject>() {
                    @Override
                    public boolean filter(JSONObject value) throws Exception {
                        String last_page = value.getJSONObject("page").getString("last_page_id");
                        return last_page == null || last_page.isEmpty();
                    }
                })
                .within(Time.seconds(10));

        PatternStream<JSONObject> patternStream  = CEP.pattern(stream, pattern);

        OutputTag<String> outputTag = new OutputTag<String>("timeOutData") {
        };

        //后续发送到kafka中，需要string类型的数据，所以将Jsonobject转化为string类型数据
        SingleOutputStreamOperator<String> selectData = patternStream.select(
                outputTag,
                //得到超时的数据,只有begin中的where匹配的数据
                new PatternTimeoutFunction<JSONObject, String>() {
                    @Override
                    public String timeout(Map<String, List<JSONObject>> map, long l) throws Exception {
                        List<JSONObject> go_in = map.get("go_in");
                        return go_in.get(0).toJSONString();
                    }
                },
                //得到匹配出的数据，有begin和next的数据，只选择第一条
                new PatternSelectFunction<JSONObject, String>() {
                    @Override
                    public String select(Map<String, List<JSONObject>> map) throws Exception {
                        List<JSONObject> go_in = map.get("go_in");
                        return go_in.get(0).toJSONString();
                    }
                }
        );

        //匹配的数据和超时的数据都是跳出的数据
        DataStream<String> jumpdata = selectData.union(selectData.getSideOutput(outputTag));

        return jumpdata;
    }
}
